package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.sys.bean.SysArticle;
import com.fzrskj.cloud.free.sys.bean.SysArticleView;
import com.fzrskj.cloud.free.sys.dao.SysArticleDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysArticleUtils extends CObject{
	
	/**
	 * 获取文章数据
	 * @param id
	 * @return
	 */
	public SysArticle getsysArticle(int id){
		SysArticleDao dao = new SysArticleDao();
		SysArticle s = dao.querySysArticle(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return s;
	}

	/**
	 * 获取协议
	 * @param flag
	 * @return
	 */
	public SysArticle getsysArticleFlag(String flag){
		SysArticleDao dao = new SysArticleDao();
		SysArticle s = dao.querySysArticleFlag(flag);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return s;
	}

	/**
	 * 修改公告
	 * @param map
	 * @return
	 */
	public int modSysArticle(Map<String, Object> map, String sid){
		SysArticleDao dao = new SysArticleDao();
		String flag = (String)map.get("flag");
		int id =(Integer)map.get("id");
		if(!CStrTool.isBlankStr((String)map.get("flag"))){
			SysArticle sa = dao.querySysArticleFlag(flag);
			String code = dao.getErrCode();
			if (!code.equals(SysErr.CODE_SUCCESS)){
				dao.dispose();
				this.setErrCode(code);
				return -1;
			}
			if(sa!=null && id !=sa.getId()){
				dao.dispose();
				this.setErrCode(SysErr.SYS_DATA_EXISTS);
				return -1;
			}
		}

		Map<String, Object> vals = new HashMap<String, Object>();
		vals.put("title", map.get("title"));
		vals.put("content", map.get("content"));
		vals.put("parentId", map.get("parentId"));
		vals.put("typeId",  map.get("typeId"));
		if (!CStrTool.isBlankStr((String)map.get("flag"))) {vals.put("flag",  map.get("flag"));}
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("id",id);

		int ret = dao.modSysArticle(vals, cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 删除公告
	 * @param id
	 * @return
	 */
	public int delSysArticle(int id){
		SysArticleDao dao = new SysArticleDao();
		SysArticle det = dao.querySysArticle(id);
		if (det == null) {
			dao.dispose();
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
			return 0;
		}
		int ret = dao.delSysArticle(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 添加公告
	 * @param vals
	 * @param sid
	 * @return
	 */
	public int addSysArticle(Map<String, Object> vals , String sid){
		SysArticleDao dao = new SysArticleDao();

		String flag = (String)vals.get("flag");
		if(!CStrTool.isBlankStr(flag)){
			SysArticle sa = dao.querySysArticleFlag(flag);
			String code = dao.getErrCode();
			if (!code.equals(SysErr.CODE_SUCCESS)){
				dao.dispose();
				this.setErrCode(code);
				return -1;
			}
			if(sa!=null){
				dao.dispose();
				this.setErrCode(SysErr.SYS_DATA_EXISTS);
				return -1;
			}
		}
		SysArticle  sysArticle = new SysArticle();
		sysArticle.setTitle((String)vals.get("title"));
		sysArticle.setContent((String)vals.get("content"));
		sysArticle.setAttachmentUrl((String)vals.get("attachmentUrl"));
		sysArticle.setParentId((String)vals.get("parentId"));
		sysArticle.setTypeId((String)vals.get("typeId"));
		sysArticle.setFlag(flag);

		sysArticle.setPublishTime(new Date());
		//需要获取Session
		sysArticle.setAuthorId(CSessionTool.getUidCloud(sid));
		//需要获取Session
		sysArticle.setAuthorName(CSessionTool.getAccountCloud(sid));
		sysArticle.setShowIndex(dao.getNewshowIndex((String)vals.get("parentId")));
		sysArticle.setState(1);
		sysArticle.setVisitors(0);
		int ret = dao.addSysArticle(sysArticle);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	/**
	 * 查询条数
	 * @param cdt
	 * @return
	 */
	public int querySysArticle(Map<String, Object> cdt){
		SysArticleDao dao = new SysArticleDao();
		int ret = dao.querySysArticle(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 分页查询文章
	 * @param cdt
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<SysArticleView> querySysArticleList(Map<String, Object> cdt, int page, int pagesize){
		SysArticleDao dao = new SysArticleDao();
		List<SysArticleView> ret =dao.querySysArticleList(cdt, page, pagesize);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 查询文章列表
	 * @param cdt
	 * @return
	 */
	public List<SysArticle> querySysArticleList(Map<String, Object> cdt){
		SysArticleDao dao = new SysArticleDao();
		List<SysArticle> ret =dao.querySysArticleList(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	//分割传过来的P参数  替换%字符
	public String splitArticle(String p){
		String str = "";
		String[] array = p.split("仝仝");
		for(int i=0;i<array.length;i++){
			str+=array[i]+"%";
		}
		 str=str.substring(0,str.length()-1);
		 return str;
	}

	/**
	 * 获取附件的文章.
	 * @param id 当前文章ID.
	 * @param direction 1-下一条，其他-上一条
	 * @param parentId 文章分类.
	 * @param typeId 文章类型.
	 * @return 文章.
	 */
	public SysArticleView getRecent(Integer id, Integer direction, String parentId, String typeId) {
		SysArticleDao dao = new SysArticleDao();
		SysArticleView ret = dao.getRecent(id, direction, parentId, typeId);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 初始化代理平台文章.
	 * @param agentId 代理商ID.
	 * @param agentName 代理商名称.
	 * @return affect line number.
	 */
	public Integer initAgent(Integer agentId, String agentName) {
		SysArticleDao dao = new SysArticleDao();
		Integer ret = dao.initAgent(agentId, agentName);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}
}